<?php

namespace app\api\controller\driver;

use app\common\constant\driving\CommonConstant;
use app\common\constant\driving\DriverMoneyLogConstant;
use app\common\controller\Api;
use app\common\service\driver\DriverMoneyLogService;
use app\common\service\driver\DriverRechargeOrderService;
use app\common\service\driver\DriverService;
use app\common\service\driver\DriverWithdrawalService;
use think\Validate;

/**
 * 司机端-我的钱包接口
 * @ApiWeigh (87)
 */
class Wallet extends Api
{
    protected $beforeActionList = [
        'recharge_before' => ['only' => 'recharge'],
        'withdrawal_before' => ['only' => 'withdrawal'],
        'get_openid_before' => ['only' => 'get_openid'],
    ];
    protected $noNeedLogin = ['type_list', 'recharge_money_list', 'paytype_list'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiInternal
     */
    public function recharge_before()
    {
        $params = $this->request->post();
        $rule = [
            'amount|充值金额' => 'require|gt:0',
            'paytype|支付方式' => 'require|in:' . CommonConstant::PAYTYPE_WECHAT . ',' . CommonConstant::PAYTYPE_ALIPAY,
        ];
        $message = [
            'paytype.require' => '请选择支付方式',
            'paytype.in' => '请选择支付方式',
        ];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
        $money = config('site.recharge_money');
        if ($params['amount'] < $money) {
            $this->error('充值金额必须大于' . $money . '元');
        }
    }

    /**
     * @ApiInternal
     */
    public function withdrawal_before()
    {
        $params = $this->request->post();
        $rule = [
            'amount|提现金额' => 'require|gt:0',
            'paytype|支付方式' => 'require|in:' . CommonConstant::PAYTYPE_WECHAT . ',' . CommonConstant::PAYTYPE_ALIPAY,
            'identity|支付宝账号' => 'requireIf:paytype,' . CommonConstant::PAYTYPE_ALIPAY,
            'name|支付宝账号姓名' => 'requireIf:paytype,' . CommonConstant::PAYTYPE_ALIPAY,
        ];
        $message = [
            'paytype.require' => '请选择提现方式',
            'paytype.in' => '请选择提现方式',
        ];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
        $money = config('site.withdrawal_money');
        if ($params['amount'] < $money) {
            $this->error('提现金额必须大于' . $money . '元');
        }
        $max = 200;
        if ($params['paytype'] == CommonConstant::PAYTYPE_WECHAT && $params['amount'] > $max) {
            $this->error('微信提现金额不能大于' . $max . '元');
        }
    }

    /**
     * @ApiInternal
     */
    public function get_openid_before()
    {
        $openid = $this->request->post('openid');
        $unionid = $this->request->post('unionid');
        if (empty($openid) || empty($unionid)) {
            $this->success('success');
        }
    }


    /**
     * 余额变动类型列表
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"余额变动类型列表","time":"1721180168","data":[{"id":1,"name":"订单收入"}]})
     **/
    public function type_list()
    {
        $this->success('余额变动类型列表', get_one_two_array(DriverMoneyLogConstant::get_type_list(), 'id', 'name'));
    }

    /**
     * 余额变动列表
     *
     * @ApiMethod (POST)
     * @ApiParams (name="page", type="integer", required=true, description="页数 不传值默认1")
     * @ApiParams (name="page_size", type="integer", required=true, description="每页数量 不传值默认10")
     * @ApiParams (name="type", type="integer", required=false, description="类型 不传值默认全部")
     * @ApiParams (name="date", type="string", required=false, description="日期 2024-07 不传值默认全部")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturnParams (name=data.total,type="integer",description=总数量)
     * @ApiReturnParams (name=data.data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"余额变动列表","time":"1721179622","data":{"total":2,"data":[{"id":2,"driver_id":1,"type":"类型","change_type":"变动类型:1=增加,2=减少","memo":"备注","createtime":"创建时间"}]}})
     **/
    public function list()
    {
        $page = $this->request->post('page');
        $page_size = $this->request->post('page_size');
        $type = $this->request->post('type');
        $date = $this->request->post('date');
        $user = $this->auth->getUser();
        $list = DriverMoneyLogService::list($page, $page_size, $type, $date, $user);
        $this->success('余额变动列表', $list);
    }

    /**
     * 充值金额列表
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"充值金额列表","time":"1721180168","data":[{"key":1,"money":"充值金额"}]})
     **/
    public function recharge_money_list()
    {
        $this->success('充值金额列表', get_one_two_array(config('site.recharge_money_list'), 'key', 'money'));
    }

    /**
     * 支付方式列表
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"支付方式列表","time":"1721180168","data":[{"id":"wechat","name":"微信支付"}]})
     **/
    public function paytype_list()
    {
        $this->success('充值金额列表', get_one_two_array(CommonConstant::get_paytype_list(), 'id', 'name'));
    }

    /**
     * 充值
     *
     * @ApiMethod (POST)
     * @ApiParams (name="amount", type="string", required=true, description="充值金额")
     * @ApiParams (name="paytype", type="string", required=true, description="支付类型:wechat=微信支付,alipay=支付宝支付")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturnParams (name=data.paytype,type="string",description="支付类型:wechat=微信支付,alipay=支付宝支付")
     * @ApiReturnParams (name=data.apptype,type="string",description="支付方法:app=APP支付")
     * @ApiReturnParams (name=data.response,type="mixed",description="支付数据")
     * @ApiReturn ([{"code": 1,"msg": "创建订单成功","time": "1721609013","data": {"paytype": "alipay","apptype": "app","response": "app_id=2021004160694881&format=JSON&charset=utf-8&sign_type=RSA2&version=1.0&return_url=https%3A%2F%2Fkzdriving.emaigu.com%2Fapi%2Fpay%2Freturnx%2Fpaytype%2Falipay%2Ftype%2Forder%2Fout_trade_no%2F12345678&notify_url=https%3A%2F%2Fkzdriving.emaigu.com%2Fapi%2Fpay%2Fnotifyx%2Fpaytype%2Falipay%2Ftype%2Forder&timestamp=2024-07-22+08%3A43%3A33&biz_content=%7B%22out_trade_no%22%3A%2212345678%22%2C%22total_amount%22%3A%221%22%2C%22subject%22%3A%22%5Cu652f%5Cu4ed8%5Cu8ba2%5Cu5355%5Cuff0c1%5Cu5143%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&app_cert_sn=0b2520ed9fc7c0300cbdaab33bce5a1c&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&method=alipay.trade.app.pay&sign=A%2BGlkTj2uXqTCSvf1O0GDwI607Mvz1R5Y%2FKWBdqaXUaY%2FoKwnI2Nm41TxrWoF0lZ1VZOUFuK1aiaeeUWox0MrMRj4eoi1HokfDSsraFznawNCp6mcEaymegYCe%2BfVEszTsAHKr9l1VUKKWmNA5MaO8dCmD7BHJKfsbS32wYagrpsqVv2CxwUA%2BMtit1uDI7S1ZTsWYFRCB8%2FTC1fSf9d5%2BskozVs3bpRrBrMBpTF4Xu7HnCOnT5T7uAisWjRtfTisVM6ux%2BofKYLImaaLpqaXe6QtoBdcF09OMjCEoZzRc%2BFEJ6JwthprmtbYh3kkIE3TlnAVHtFaAFhvd4jHeExGg%3D%3D"}},{"code": 1,"msg": "创建订单成功","time": "1700978950","data": {"paytype": "wechat","apptype": "app","response": {"appId": "wx201d569af61f5db0","timeStamp": "1700978950","nonceStr": "53tzoGksxhVkJBvH","package": "prepay_id=wx26140910426148a768c351a84db0db0000","signType": "MD5","paySign": "80AAD89D0684FFF4F5AC51E513F115FF"}}}])
     **/
    public function recharge()
    {
        $amount = $this->request->post('amount');
        $paytype = $this->request->post('paytype');
        $apptype = 'app';
        $user = $this->auth->getUser();
        $result = DriverRechargeOrderService::create($amount, $paytype, $apptype, $user);
        $this->success('支付信息', $result);
    }

    /**
     * 提现
     *
     * @ApiMethod (POST)
     * @ApiParams (name="amount", type="string", required=true, description="提现金额")
     * @ApiParams (name="paytype", type="string", required=true, description="支付类型:wechat=微信支付,alipay=支付宝支付")
     * @ApiParams (name="identity", type="string", required=false, description="支付宝账号")
     * @ApiParams (name="name", type="string", required=false, description="支付宝账号姓名")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "提交成功","time": "1695260676","data": null})
     **/
    public function withdrawal()
    {
        $amount = $this->request->post('amount');
        $paytype = $this->request->post('paytype');
        $identity = $this->request->post('identity');
        $name = $this->request->post('name');
        $user = $this->auth->getUser();
        $result = DriverWithdrawalService::create($amount, $paytype, $identity, $name, $user);
        $this->success($result == true ? '提交成功' : '提交成功，请耐心等待审核');
    }

    /**
     * 提现 - 微信授权
     *
     * @ApiMethod (POST)
     * @ApiParams (name="openid", type="string", required=true, description="openid")
     * @ApiParams (name="unionid", type="string", required=true, description="unionid")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "微信授权成功","time": "1695260676","data": null})
     **/
    public function get_openid()
    {
        $params = $this->request->post();
        $user = $this->auth->getUser();
        DriverService::get_openid($params, $user);
        $this->success('微信授权成功');
    }

}
